package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.AddressAadapter;
import com.hemaapp.yjnh.bean.Address;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;

public class AddressSelectActivity extends BaseActivity implements OnClickListener {

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private RefreshLoadmoreLayout layout;
	private ListView listview;
	private ProgressBar progressbar;

	private AddressAadapter adapter;

	private String parent_id = "0";
	private Integer page = 0;

	private String type = "";
	private ArrayList<Address> addresss = new ArrayList<Address>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_addressselect);
		super.onCreate(savedInstanceState);
		init();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub

		super.onDestroy();
	}

	private void init() {
		// TODO Auto-generated method stub
		getDistricts();
	}

	private void getDistricts() {
//		getNetWorker().districtList(parent_id);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.left:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		// TODO Auto-generated method stub
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		// TODO Auto-generated method stub
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case DISTRICT_LIST:
			progressbar.setVisibility(View.GONE);
			layout.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		// TODO Auto-generated method stub
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case DISTRICT_LIST:
			HemaPageArrayResult<Address> gResult = (HemaPageArrayResult<Address>) baseResult;
			if (page == 0) {
				layout.refreshSuccess();
				addresss.clear();
				addresss.addAll(gResult.getObjects());

				if (gResult.getObjects().size() < BaseApplication
						.getInstance().getSysInitInfo().getSys_pagesize()) {
					layout.setLoadmoreable(false);
				} else {
//					layout.setLoadmoreable(true);
				}
			} else { // 更多
				layout.loadmoreSuccess();
				if (gResult.getObjects().size() > 0) {
					addresss.addAll(gResult.getObjects());
				} else {
					layout.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}

			if (adapter == null) {
				adapter = new AddressAadapter(mContext, addresss);
				listview.setAdapter(adapter);
			} else {
				adapter.notifyDataSetChanged();
			}
			break;

		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		// TODO Auto-generated method stub
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case DISTRICT_LIST:
			showTextDialog(baseResult.getMsg());
			if (page == 0) {
				layout.refreshFailed();
			} else {
				layout.loadmoreFailed();
			}

			break;

		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		// TODO Auto-generated method stub
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case DISTRICT_LIST:
			showTextDialog("获取失败");
			if (page == 0) {
				layout.refreshFailed();
			} else {
				layout.loadmoreFailed();
			}
			break;

		default:
			break;
		}
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		
		layout = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (ListView) findViewById(R.id.listview);
		progressbar = (ProgressBar) findViewById(R.id.progressbar);
	}

	@Override
	protected void getExras() {
		// TODO Auto-generated method stub
		parent_id = mIntent.getStringExtra("parent_id");
		type = mIntent.getStringExtra("type");
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		titleLeft.setOnClickListener(this);
		titleRight.setVisibility(View.GONE);
		if ("1".equals(type)) {
			titleText.setText("选择省份");
		} else if ("2".equals(type)) {
			titleText.setText("选择城市");
		} else {
			titleText.setText("选择地区");
		}
		layout.setOnStartListener(onStartListener);
		layout.setLoadmoreable(false);
	}

	private OnStartListener onStartListener = new OnStartListener() {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
			// TODO Auto-generated method stub
			page = 0;
			getDistricts();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
			// TODO Auto-generated method stub
			page++;
			getDistricts();
		}
	};
}
